package com.server.modules.api.web;

import com.google.gson.Gson;
import com.server.modules.api.Base.BaseAppController;
import com.server.modules.api.vo.BaseQueryVo;
import com.server.modules.api.vo.list.ContactList;
import com.server.modules.api.vo.ContactVo;
import com.server.modules.sys.entity.member.Member;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Author SilentWolf
 * Date ${DATETIME}
 */
@Controller
@RequestMapping(value = "/api/contact")
public class ContactAPIController extends BaseAppController {



	@ResponseBody
	@RequestMapping(value = "/uploadContacts", method = RequestMethod.POST)
	public ContactVo uploadContacts(HttpServletRequest request, BaseQueryVo queryVo) {
		ContactVo<ContactList> vo = new ContactVo<ContactList>();

		String contact = request.getParameter("contact");

		if (StringUtils.isEmpty(contact)) {
			vo.setRetCode(100);
			vo.setRetMsg("上传数据为空");
			return vo;
		}
		//捕获Json数据转换异常
		try {
			ContactList contactList = new Gson().fromJson(contact, ContactList.class);
			if (queryVo.getMemberId()!=null)
			contactAPIService.insertContact(contactList,queryVo.getMemberId());
			else {
				Member member =memberAPIService.selectMemberBySessionId(queryVo.getSessionId());
				if (member!=null)
				contactAPIService.insertContact(contactList,member.getId());
				else contactAPIService.insertContact(contactList,null);
			}
			vo.setRetCode(0);
			vo.setRetMsg("通讯录上传成功");
		} catch (Exception e) {
			vo.setRetCode(120);
			vo.setRetMsg("通讯录上传失败");
		}

		return vo;
	}
}
